package com.zby.demo.controller.back;

import com.zby.demo.dao.ClassDao;
import com.zby.demo.dao.CourseDao;
import com.zby.demo.entity.Class;
import com.zby.demo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/23 15:04
 */

@Controller
public class ClassController {
    @Autowired
    ClassDao classDao;
    @Autowired
    CourseDao courseDao;

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:28
     * @Description: 转发到班级列表页面
     */
    @RequestMapping("/back/class/list")
    public String toClassList(Model model) {
        List<Class> classes = classDao.queryAllClass();
        model.addAttribute("classes", classes);
        return "back/class/list";
    }

    /*
     * @Param: [addclass]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:28
     * @Description: 添加班级
     */
    @PostMapping("/back/class/add")
    public String addClass(Class addclass) {
        classDao.addClass(addclass);
        return "redirect:/back/class/list";
    }

    /*
     * @Param: [classId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:29
     * @Description: 删除班级。注意点：删除班级前需要删除本班所有学生，删除学生之前需要删除学生的选课信息。
     */
    @GetMapping("/back/class/delete/{classId}")
    public String deleteClass(@PathVariable("classId") String classId) {
        for (Student student : classDao.queryStudentsInClass(classId)) {
            courseDao.deleteSelectCourseById(student.getStudentId());
        }
        classDao.deleteClassStudents(classId);
        classDao.deleteClass(classId);
        return "redirect:/back/class/list";
    }
}
